var qiniukeys = [];
var qiniuFormat = {};
var fsize=[];

var uploader = Qiniu.uploader({
    runtimes: 'html5,flash,html4',    //上传模式,依次退化
    browse_button: 'pickfiles',       //上传选择的点选按钮，**必需**
    uptoken_url: local+'/file/qiniu/uptoken',            //Ajax请求upToken的Url，**强烈建议设置**（服务端提供）
    // uptoken : uptoken, //若未指定uptoken_url,则必须指定 uptoken ,uptoken由其他程序生成
    // unique_names: true, // 默认 false，key为文件名。若开启该选项，SDK为自动生成上传成功后的key（文件名）。
    // save_key: true,   // 默认 false。若在服务端生成uptoken的上传策略中指定了 `sava_key`，则开启，SDK会忽略对key的处理
    domain: 'http://media.ebowin.com/',   //bucket 域名，下载资源时用到，**必需**
    get_new_uptoken: false,  //设置上传文件的时候是否每次都重新获取新的token
    container: 'container',           //上传区域DOM ID，默认是browser_button的父元素，
    max_file_size: '2048mb',           //最大文件体积限制
    flash_swf_url: '../plupload/Moxie.swf',  //引入flash,相对路径
    max_retries: 3,                   //上传失败最大重试次数
    dragdrop: true,                   //开启可拖曳上传
    drop_element: 'container',        //拖曳上传区域元素的ID，拖曳文件或文件夹后可触发上传
    chunk_size: '4mb',                //分块上传时，每片的体积
    auto_start: true,
    multi_selection: false,//选择文件后自动上传，若关闭需要自己绑定事件触发上传
/*    filters : {
        max_file_size : '2048mb',
        prevent_duplicates: true,
        mime_types: [
                     
                     {title : "Video files", extensions : "flv,mp4,mp3"}, // 限定flv,mpg,mpeg,avi,wmv,mov,asf,rm,rmvb,mkv,m4v,mp4后缀格式上传
                     {title : "file files", extensions : "flv,doc,docx,ppt,xls,xlsc"} //限定zip后缀上传
                  {title : "Video files", extensions : "flv"} // 限定flv后缀上传格式上传
        ]
    },*/
    init: {
        'FilesAdded': function(up, files) {
            $('table').show();
            $('#qiniuUploadSuccess').hide();
            plupload.each(files, function(file) {
                var progress = new FileProgress(file, 'fsUploadProgress');
                progress.setStatus("等待...");
                progress.bindUploadCancel(up);
            });
        },
        'BeforeUpload': function(up, file) {
            var progress = new FileProgress(file, 'fsUploadProgress');
			var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
			if (up.runtime === 'html5' && chunk_size) {
			    progress.setChunkProgess(chunk_size);
			}
        },
        'UploadProgress': function(up, file) {
            var progress = new FileProgress(file, 'fsUploadProgress');
            var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
            progress.setProgress(file.percent + "%", file.speed, chunk_size);
        },
        'FileUploaded': function(up, file, info) {
            
        	var str = file.name.split(".");
    		var suffix = str[str.length-1];
    		if(suffix!="mp4" && suffix!="flv"){
    			doQiniuAvinfo = false;
    		}
        	
        	var progress = new FileProgress(file, 'fsUploadProgress');
        	/**
             * 获取上传完成的详细信息
             */
        	if(doQiniuAvinfo){
        		progress.avinfo(up, info);
        	}
            
            qiniukeys.push(progress.setComplete(up, info));//得到上传文件的key的数组;
            fsize.push(file.size);
            
        },
        'Error': function(up, err, errTip) {
            $('table').show();
            var progress = new FileProgress(err.file, 'fsUploadProgress');
            progress.setError();
            progress.setStatus(errTip);
        },
        'UploadComplete': function() {
            $('#qiniuUploadSuccess').show();
        },
        'Key': function(up, file) {
			var t = $.md5(JSON.stringify(file));
            var key = "p-admin-" + t.substring(t.length-8,t.length)  + "-" + file.name;
            return key;
        }
    }
});

/**
 * 视频资源目前只能传一个
 */

var initQiniukeys = function(){
	if(qiniukeys.length==0)return false;
	
	if(doQiniuAvinfo && qiniuFormat.duration){
		/**
		 * 创建时长
		 */
		var $input_duration = $("<input>",{type:'hidden',val:qiniuFormat.duration,name:"playTimeSeconds"});
	
		$('#qiniuUploadSuccess').append($input_duration);
		
	}
	/**
	 * 创建文件key
	 */
	var $input_key = $("<input>",{type:'hidden',val:qiniukeys[0],name:"qiniuKey"});
	/**
	 * 创建文件大小
	 */
	var $input_fsize = $("<input>",{type:'hidden',val:fsize[0],name:"fsize"});
	$('#qiniuUploadSuccess').append($input_fsize);
	$('#qiniuUploadSuccess').append($input_key)
	
	return true;
}

